﻿using System;
using System.Threading;
using YArchitech.LIB;
//using Hongye.Library.LicensePub;

namespace YArchitech.Public.Encrypt
{
	internal class AuthorizeLicenseChecker : LicenseChecker
	{
		public static int CheckSecond
		{
			get
			{
				return AuthorizeLicenseChecker.m_CheckSecond;
			}
			set
			{
				AuthorizeLicenseChecker.m_CheckSecond = value;
			}
		}

		public AuthorizeLicenseChecker(int checkSecond = 300, int maxError = 5)
		{
			AuthorizeLicenseChecker.m_CheckSecond = checkSecond;
			this.m_MaxErrorCount = maxError;
			this.m_LicenseValid = false;
			this.m_ErrorCount = 0;
		}

		public void StartTimer()
		{
			this.EndTimer();
			this.m_CheckThread = new Thread(new ParameterizedThreadStart(AuthorizeLicenseChecker.CheckLicenseThread));
			this.m_CheckThread.Start(this);
		}

		public void EndTimer()
		{
			if (this.m_CheckThread != null)
			{
				this.m_CheckThread.Abort();
			}
		}

		public static void CheckLicenseThread(object data)
		{
			AuthorizeLicenseChecker authorizeLicenseChecker = (AuthorizeLicenseChecker)data;
			if (authorizeLicenseChecker == null)
			{
				return;
			}
			for (;;)
			{
				Thread.Sleep(AuthorizeLicenseChecker.m_CheckSecond * 1000);
				authorizeLicenseChecker.CheckLicense();
			}
		}

		public override void ReleaseLicense()
		{
            //if (this.ls.Logout())
            //{
            //    this.m_ErrorMessage = "Logout success";
            //    return;
            //}
            //this.m_ErrorMessage = "Logout failed \n" + this.ls.ErrorDescribe;
		}

		public override bool GetLicense()
		{
            return true;

            //string host = this.GetHost();
            //LicensePub.LockType lockType = this.GetLockType();
            //if (this.GetRunType() == 0)
            //{
            //    this.m_LicenseValid = this.ls.LoginEx(34u, ref host);
            //}
            //else
            //{
            //    this.m_LicenseValid = this.ls.LoginEx(base.ProductId, ref host);
            //}
            //if (!this.m_LicenseValid)
            //{
            //    this.m_ErrorMessage = "没有检测到许可证！";
            //    return false;
            //}
            //this.SaveLockType(lockType);
            //this.SaveHost(host);
            //this.StartTimer();
            //return this.m_LicenseValid;
		}

		public override bool CheckLicense()
		{
            //this.m_LicenseValid = this.ls.CheckSession();
            //if (!this.m_LicenseValid)
            //{
            //    this.m_ErrorCount++;
            //    this.m_ErrorMessage = "许可证错误！";
            //}
            //else
            //{
            //    this.m_ErrorCount = 0;
            //}
            //if (this.m_ErrorCount >= this.m_MaxErrorCount)
            //{
            //    base.OnLicenseError();
            //}
            //return this.m_LicenseValid;

            return true;
		}

		private string GetHost()
		{
			return IniHelper.GetSetting("Product", "ContactServer", Product.ConfigFile, this.LocalHost);
		}

		private int GetRunType()
		{
			return Convert.ToInt32(IniHelper.GetSetting("Product", "RunType", Product.DefaultConfigFile, "1"));
		}

		private void SaveHost(string host)
		{
			host.ToLower();
			IniHelper.WriteSetting("Product", "ContactServer", host, Product.ConfigFile);
			if (string.Compare(host, this.LocalHost, true) == 0)
			{
				Product.SaveSoftWareType(2);
				return;
			}
			Product.SaveSoftWareType(3);
		}

        //private LicensePub.LockType GetLockType()
        //{
        //    int result = 1;
        //    int.TryParse(IniHelper.GetSetting("Product", "LockType", Product.ConfigFile, "1"), out result);
        //    return (LicensePub.LockType)result;
        //}

        //private void SaveLockType(LicensePub.LockType lt)
        //{
        //    IniHelper.WriteSetting("Product", "LockType", (int)lt, Product.ConfigFile);
        //}

        //private readonly LicensePub ls = new LicensePub();

		private Thread m_CheckThread;

		private int m_ErrorCount;

		private static int m_CheckSecond = 300;

		protected int m_MaxErrorCount = 5;

		private readonly string LocalHost = "localhost".ToLower();
	}
}
